cmake_minimum_required(VERSION 3.16 FATAL_ERROR)

#This check should occur before project()
find_program(CCACHE_PROGRAM ccache)
if(CCACHE_PROGRAM)
    set_property(GLOBAL PROPERTY RULE_LAUNCH_COMPILE "${CCACHE_PROGRAM}")
endif()

##项目信息
project(CAXP)
set(PACKAGE_VERSION_MAJOR "1")
set(PACKAGE_VERSION_MINOR "0")
set(PACKAGE_VERSION_PATCH "0")
set(PACKAGE_VERSION "${PACKAGE_VERSION_MAJOR}.${PACKAGE_VERSION_MINOR}.${PACKAGE_VERSION_PATCH}")
set(PACKAGE_STRING "${PROJECT_NAME} ${PACKAGE_VERSION}")

##代码路径
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/bin/$<CONFIG>")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY "${CMAKE_SOURCE_DIR}/lib/$<CONFIG>")

set(INCLUDE_FILE_PATH ${CMAKE_SOURCE_DIR}/include)
set(SOURCE_FILE_PATH ${CMAKE_SOURCE_DIR}/src)
set(THIRD_PARTY_LIBPACK_DIR ${CMAKE_SOURCE_DIR}  CACHE PATH  "Directory of the 3rdparty libpack")

##配置构建类型
set(CMAKE_CONFIGURATION_TYPES "Debug;Release;RelWithDebInfo") 
##使用"文件夹"结构
set_property(GLOBAL PROPERTY USE_FOLDERS ON)
##配置C++版本
set(CMAKE_CXX_STANDARD 17)
##设置Debug版本的文件结尾符
set(CMAKE_DEBUG_POSTFIX d)

##MSVC设置
if(MSVC)
    OPTION(BUILD_RELEASE_PDB "Create PDB files for Release version." OFF)
    OPTION(BUILD_RELEASE_SEH "Enable Structured Exception Handling for Release version." OFF)
    OPTION(BUILD_USE_PCH "Activate precompiled headers where it's used." ON)
    
    #set (CMAKE_SHARED_LINKER_FLAGS "${CMAKE_SHARED_LINKER_FLAGS} /NODEFAULTLIB")
    #set (CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /NODEFAULTLIB")
    #set (CMAKE_EXE_LINKER_FLAGS "${CMAKE_EXE_LINKER_FLAGS} /NODEFAULTLIB")

    IF(BUILD_RELEASE_PDB)
        set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /Zi /Od")
        set(CMAKE_EXE_LINKER_FLAGS_RELEASE "${CMAKE_EXE_LINKER_FLAGS_RELEASE} /DEBUG")
        set(CMAKE_SHARED_LINKER_FLAGS_RELEASE "${CMAKE_SHARED_LINKER_FLAGS_RELEASE} /DEBUG")
    ENDIF(BUILD_RELEASE_PDB)

    IF(IBE_RELEASE_SEH)
        SET (CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE} /EHa")
    ENDIF(IBE_RELEASE_SEH)

    ##并行编译
    set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /MP /W3")
endif(MSVC)

##忽略告警
if(WIN32)
    add_compile_options(/wd4244)        #const类型转换
    add_compile_options(/wd4819)        #Unicode 格式
    add_compile_options(/wd4251)        #模板类型导出
    ##add_compile_options(/FS)       #disable C1041
endif()

##第三方库配置
list(APPEND CMAKE_PREFIX_PATH ${THIRD_PARTY_LIBPACK_DIR})
if(NOT EXISTS ${THIRD_PARTY_LIBPACK_DIR})
    message(FATAL_ERROR "Thirdparty library path not found")
endif()

IF(EXISTS ${THIRD_PARTY_LIBPACK_DIR}/Qt/lib/Qt5Core.lib)
    set(Qt5_DIR "${THIRD_PARTY_LIBPACK_DIR}/Qt/lib/cmake/Qt5" CACHE PATH "Qt5 directory")
ELSE()
    set(Qt5_DIR "" CACHE PATH "Qt5 directory")
ENDIF()
find_package(Qt5 COMPONENTS Core Gui Widgets Concurrent) 

if(EXISTS ${THIRD_PARTY_LIBPACK_DIR}/OpenCASCADE/lib/TKernel.lib)
    set(OpenCASCADE_DIR "${THIRD_PARTY_LIBPACK_DIR}/OpenCASCADE" CACHE PATH "OpenCASCADE directory")
else()
    set(OpenCASCADE_DIR "" CACHE PATH "OpenCASCADE directory")
endif()
list(APPEND CMAKE_PREFIX_PATH ${OpenCASCADE_DIR})
find_package(OpenCASCADE REQUIRED COMPONENTS FoundationClasses ModelingData ModelingAlgorithms) 

IF(EXISTS ${THIRD_PARTY_LIBPACK_DIR}/OpenSceneGraph/lib/osg.lib)
    set(OSG_DIR "${THIRD_PARTY_LIBPACK_DIR}/OpenSceneGraph/" CACHE PATH "OSG directory")
ELSE()
    set(OSG_DIR "" CACHE PATH "OSG directory")
ENDIF()
find_package(OpenSceneGraph REQUIRED COMPONENTS osg osgText osgViewer osgGA)

IF(EXISTS ${THIRD_PARTY_LIBPACK_DIR}/Coin3D/lib/Coin4.lib)
    set(Coin3D_DIR "${THIRD_PARTY_LIBPACK_DIR}/Coin3D/" CACHE PATH "Coin3D directory")
ELSE()
    set(Coin3D_DIR "" CACHE PATH "Coin3D directory")
ENDIF()
list(APPEND CMAKE_PREFIX_PATH ${Coin3D_DIR})
find_package(Coin3D REQUIRED)

if(EXISTS ${THIRD_PARTY_LIBPACK_DIR}/GTest/lib/gtest.lib)
    set(GTest_DIR "${THIRD_PARTY_LIBPACK_DIR}/GTest/lib/cmake/GTest" CACHE PATH "GTest_DIR directory")
else()
    set(GTest_DIR "" CACHE PATH "GTest_DIR directory")
endif()
list(APPEND CMAKE_PREFIX_PATH ${GTest_DIR})
find_package(GTest REQUIRED)

##安装路径
if(WIN32)
    set(CMAKE_INSTALL_PREFIX "${CMAKE_SOURCE_DIR}/sdk" CACHE PATH "Installation root directory")
else(WIN32)
    set(CMAKE_INSTALL_PREFIX "/usr/lib${LIB_SUFFIX}/caxp" CACHE PATH "Installation root directory")
endif(WIN32)

install(DIRECTORY ${INCLUDE_FILE_PATH} DESTINATION ${CMAKE_INSTALL_PREFIX})

##源码
add_subdirectory(src)
